package Databases;

import org.xmldb.api.base.*;
import org.xmldb.api.*;
import org.xmldb.api.base.Database;

public class ImpXmlDbDataSource implements XmlDbDataSource {

    protected static String driver = "XmlDbDataSource/driverClassName";
    protected static String URI = "XmlDbDataSource/url";
    protected static String username = "XmlDbDatasource/user";
    protected static String password = "XmlDbDatasource/password";
    protected static String cname = "XmlDbDatasource/cname";
    ReadProperties property = null;
    private String loginproperty = "DatabaseLogin.properties";
    Collection col = null;

    public ImpXmlDbDataSource() throws XMLDBException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        property = ReadProperties.getInstance(loginproperty);
        Class cl = Class.forName(property.getProperty(driver));
        Database database = (Database) cl.newInstance();
        DatabaseManager.registerDatabase(database);
        database.setProperty("create-database", "true");
    }

    public Collection getCollection() throws XMLDBException {
        col = DatabaseManager.getCollection(property.getProperty(URI) + property.getProperty(cname));
        col.setProperty("encoding", "ISO-8859-1");
        return col;
    }

    public Collection getCollection(String aUserName, String aPassword) throws XMLDBException {
        col = DatabaseManager.getCollection(property.getProperty(URI) + property.getProperty(cname), aUserName, aPassword);
        col.setProperty("encoding", "ISO-8859-1");
        return col;
    }
}